package com.mrobuy.jd.api.primarypic;

import com.gavel.database.SQLExecutor;
import com.google.gson.Gson;
import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.request.supplier.VcItemFeatureFindRequest;
import com.jd.open.api.sdk.response.supplier.VcItemFeatureFindResponse;
import com.mrobuy.jd.api.JosClient;
import com.mrobuy.jd.sdk.request.VcItemPrimaryPicAppliesFindRequest;
import com.mrobuy.jd.sdk.response.VcItemPrimaryPicAppliesFindResponse;
import com.mrobuy.jdvc.entity.VCFeature;
import com.mrobuy.jdvc.entity.VCPrimaryPicApply;
import com.mrobuy.jdvc.entity.VCProductInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *  获取类目下开通品牌信息
 *
 *  jingdong.vc.item.products.find
 *
 *  <url>https://open.jd.com/home/home#/doc/api?apiCateId=86&apiId=1681&apiName=jingdong.vc.item.products.find</url>
 *
 *
 *
 */
public class PrimaryPicAppliesFindClient extends JosClient<List<VCPrimaryPicApply>> {

    private final VcItemPrimaryPicAppliesFindRequest request=new VcItemPrimaryPicAppliesFindRequest();

    private static final  int PAGE_SIZE = 50;

    public PrimaryPicAppliesFindClient(JdClient client) {
        super(client);
    }

    @Override
    public List<VCPrimaryPicApply> call() throws Exception {

        int page = 1;
        int total = Integer.MAX_VALUE;


        List<VCPrimaryPicApply> applies = new ArrayList<>();

        while ( page * PAGE_SIZE < total ) {

            try {
                request.setPage(page);
                request.setLength(PAGE_SIZE);

                VcItemPrimaryPicAppliesFindResponse response = client.execute(request);

                total = response.getJosResultDto().getCount();

                com.jd.open.api.sdk.domain.supplier.ProductManagementService.response.find.ItemPicApplyDto[] results = response.getJosResultDto().getResult();
                if ( results!=null && results.length > 0 ) {
                    for (com.jd.open.api.sdk.domain.supplier.ProductManagementService.response.find.ItemPicApplyDto result : results) {

                        VCPrimaryPicApply apply = new VCPrimaryPicApply();

                        apply.setApplyId(result.getApplyId());
                        apply.setApplyTime(result.getApplyTime());
                        apply.setWareId(result.getWareId());
                        apply.setName(result.getName());

                        apply.setSaleState(result.getSaleState());
                        apply.setState(result.getState());
                        apply.setBrandId(result.getBrandId());
                        apply.setBrandName(result.getBrandName());
                        apply.setCategoryId(result.getCategoryId());
                        apply.setCategoryName(result.getCategoryName());
                        applies.add(apply);
                    }
                }

                page++;
            } catch (Exception e) {

            }
        }

        return applies;
    }

    public PrimaryPicAppliesFindClient setWareId(String ware_id) {
        request.setWareId(ware_id);
        return this;
    }

    public PrimaryPicAppliesFindClient setName(String name) {
        request.setName(name);
        return this;
    }

    public PrimaryPicAppliesFindClient setBrandId(int brandId) {
        request.setBrandId(brandId);
        return this;
    }

    public PrimaryPicAppliesFindClient setCategoryId(int cateId) {
        request.setCategoryId(cateId);
        return this;
    }

    public PrimaryPicAppliesFindClient setState(int state) {
        request.setState(state);
        return this;
    }

    public PrimaryPicAppliesFindClient setBeginApplyTime(String begin_apply_time) {
        request.setBeginApplyTime(begin_apply_time);
        return this;
    }
}
